public class Test {

    private static void isSort(TreeNode root) {
        if(root == null) {
            return;
        }

        isSort(root.left);
        System.out.print(root.val + " ");
        isSort(root.right);
    }

    private static boolean isRedNodes(TreeNode root) {
        if(root == null) {
            return true;
        }

        if(root.color == Color.RED && root.parent.color == Color.RED) {
            System.out.println("出现两个连续的红色结点！！！");
            return false;
        }

        return isRedNodes(root.left) && isRedNodes(root.right);
    }

    private static  boolean isSamePathBlackNodes(TreeNode root,int pathBlack, int blackNodes) {
        if(root == null) {
            return true;
        }

        if(root.color == Color.BLACK) {
            pathBlack++;
        }

        if(root.left == null && root.right == null) {
            if(pathBlack != blackNodes) {
                System.out.println("路径上的黑色结点数目不相同！！！");
                return false;
            }
        }

        return isSamePathBlackNodes(root.left,pathBlack,blackNodes)
                && isSamePathBlackNodes(root.right,pathBlack,blackNodes);
    }

    private static boolean isRBTree (TreeNode root) {
        if(root == null) {
            return true;
        }

        if(root.color != Color.BLACK) {
            System.out.println("根节点为红色！！！");
            return false;
        }

        TreeNode cur = root;
        int blackNodes = 0;
        while(cur != null) {
            if(cur.color == Color.BLACK) {
                blackNodes++;
            }
            cur = cur.left;
        }

        return isRedNodes(root) && isSamePathBlackNodes(root,0,blackNodes);
    }

    public static void main(String[] args) {
        RedBlackTree redBlackTree = new RedBlackTree();
        int[] arr = {24,12,13,18,25,17,30,28};
        for (int i = 0; i < arr.length; i++) {
            redBlackTree.insert(arr[i]);
        }

        isSort(redBlackTree.root);
        System.out.println();
        System.out.println(isRBTree(redBlackTree.root));
    }
}
